package com.jd.mysql.mgr.controller.mgrmachine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jd.mysql.mgr.pojo.MgrMachine;
import com.jd.mysql.mgr.service.IMgrMachineService;
import com.jd.mysql.mgr.util.GlobalStaticVariables;
import com.jd.mysql.mgr.util.PageUtil;
import com.jd.mysql.mgr.util.ParamUtil;

public class MachineListController extends SimpleFormController {
	private static Integer start=0;
	private static Integer limit=GlobalStaticVariables.limit;
	private IMgrMachineService mgrMachineService;
	

	public MachineListController() {
		// TODO Auto-generated constructor stub
	}

	public void setMgrMachineService(IMgrMachineService mgrMachineService) {
		this.mgrMachineService = mgrMachineService;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		try{
			String ip = ParamUtil.getStringParam(request, "ip");
			String index = request.getParameter("index");
			String msg = (String) request.getAttribute("msg");
			Map<String,Object> map = new HashMap<String,Object>();
			Integer start = PageUtil.setPageParam(map,index, limit);
			HttpSession session = request.getSession();
			Integer groupId  = (Integer)session.getAttribute("group");
			Long totalCount = mgrMachineService.getTotalCount(groupId,true);
			List<MgrMachine> dtoList = mgrMachineService.getMgrMachineList(start, limit, true, ip,groupId);
			map.put("totalCount", totalCount);
			map.put("dtoList",dtoList);
			map.put("msg", msg);
			map.put("ip", ip==null?"":ip);
			return new ModelAndView("machine/machineList","map",map);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}
